package com.bougaga.opensource.scheduler.example.handler;

import com.bougaga.opensource.scheduler.core.handler.TaskHandler;
import com.bougaga.opensource.scheduler.core.handler.TaskResult;
import com.bougaga.opensource.scheduler.core.task.DelayedTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 示例任务处理器
 * 
 * @author bougaga
 * @since 1.0.0
 */
@Component
public class ExampleTaskHandler implements TaskHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(ExampleTaskHandler.class);
    
    @Override
    public String getHandlerName() {
        return "exampleTaskHandler";
    }
    
    @Override
    public String getSupportedTaskType() {
        return "NOTIFICATION";
    }
    
    @Override
    public TaskResult execute(DelayedTask task) {
        logger.info("Executing example task: {}", task.getTaskId());
        
        try {
            // 模拟任务执行
            Thread.sleep(1000);
            
            logger.info("Example task completed successfully: {}", task.getTaskId());
            return TaskResult.success("Example task executed successfully", task.getTaskData());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.error("Example task interrupted: {}", task.getTaskId(), e);
            return TaskResult.failure("Task interrupted", e);
        } catch (Exception e) {
            logger.error("Example task failed: {}", task.getTaskId(), e);
            return TaskResult.failure("Task execution failed", e);
        }
    }
    
    @Override
    public void beforeExecute(DelayedTask task) {
        logger.info("Before executing example task: {}", task.getTaskId());
    }
    
    @Override
    public void afterExecute(DelayedTask task, TaskResult result) {
        logger.info("After executing example task: {}, result: {}", task.getTaskId(), result.isSuccess());
    }
    
    @Override
    public void onException(DelayedTask task, Exception exception) {
        logger.error("Exception occurred while executing example task: {}", task.getTaskId(), exception);
    }
}
